﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VentaElectrodomesticos.Sql;
using VentaElectrodomesticos.Utilities;
using VentaElectrodomesticos.AsignacionStock;
//using VentaElectrodomesticos.AsignacionStock;

namespace VentaElectrodomesticos.AbmProducto
{
    public partial class FormSeleccionCategoria : Form
    {
        private Form formReceptor;
        String tipoReceptor;
        ClaseSQL sql = new ClaseSQL();

        public FormSeleccionCategoria(Form _formReceptor, String _tipoReceptor)
        {
            InitializeComponent();
            this.formReceptor = _formReceptor;
            this.tipoReceptor = _tipoReceptor;
                        
            this.LoadTreeview();
            
        }

        private void LoadTreeview()
        {

            // This code fills a DataTable with an SQL Query
            sql.Open();
            DataTable table = sql.getDataTable("SELECT idCategoria, nomCategoria, idPadre FROM LAG.productos_categorias");
            sql.Close();
            // Fill the TreeView with database data. Use null

            // as parentid for top level

            AddKids ("0", "idPadre = 0", table);

        }

        private void AddKids(string parentid, string filter, DataTable table)
        {
            DataRow[] foundRows = table.Select(filter);

            if (foundRows.Length == 0)
                return;

            // Get TreeNode of parent using Find which looks in the name

            // property of each node. true itterates all children

            TreeNode[] parentNode = treeView1.Nodes.Find(parentid, true);

            if ((!parentid.Equals("0")) && (parentNode.Length == 0))
                return;

            // Add each row to tree

            for (int i = 0; i <= foundRows.GetUpperBound(0); i++)
            {
                string nodetext = foundRows[i]["nomCategoria"].ToString();
                string nodeid = foundRows[i]["idCategoria"].ToString();

                TreeNode node = new TreeNode();

                node.Text = nodetext;
                node.Name = nodeid; // This is critical as the Find method searches the Name property

                if (parentid == "0")
                    treeView1.Nodes.Add(node); // Top Level
                else
                    parentNode[0].Nodes.Add(node); // Add children under parent

                // Itterate into any nodes
                AddKids(nodeid, "idPadre = " + nodeid, table);

            }

        }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node != null)
            {
                KeyValuePair categoria = new KeyValuePair(node.Name, node.Text);

                if (tipoReceptor.Equals("altaProducto"))
                {
                    FormAltaProducto formAltaProducto = (FormAltaProducto)formReceptor;
                    formAltaProducto.recibirCategoria(categoria);
                }
                if (tipoReceptor.Equals("seleccionProducto"))
                {
                    FormSeleccionProducto formSeleccionProducto = (FormSeleccionProducto)formReceptor;
                    formSeleccionProducto.recibirCategoria(categoria);
                }
                if (tipoReceptor.Equals("asignacionDeStock"))
                {
                    FormAsignacionStock formAsignacionStock = (FormAsignacionStock)formReceptor;
                    formAsignacionStock.recibirCategoria(categoria);
                }
                this.Close();
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
